<template>
  <div>
    <!---搜索表单-->
    <div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="12">
            <el-form-item label="搜索理财产品">
              <el-input
                v-model="form.fname"
                style="width: 100%"
                placeholder="用户名"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display:flex">
          <el-button type="primary" size="small" @click="searchSysUser">
            搜索
          </el-button>
          <el-button size="small" @click="resetData">重置</el-button>
          <el-button type="success" size="small" @click="myFunds">
            我的理财产品
          </el-button>
        </el-row>
      </el-form>
    </div>

    <!---懒加载的树形表格-->
    <el-table
      :data="list"
      style="width: 100%"
      row-key="id"
      border
      lazy
      :load="fetchSysUser"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="fname" label="理财名称" />
      <el-table-column prop="imageUrl" label="图标" #default="scope">
        <img :src="scope.row.remark" width="50" />
      </el-table-column>
      <el-table-column prop="mouthrate" label="月化率" />
      <el-table-column prop="yearrate" label="年化率" />
      <el-table-column label="操作" #default="scope">
        <el-button type="primary" @click="findFundsInfo(scope.row)">
          详情
        </el-button>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogVisible" title="产品详细" width="70%">
      <el-table
        :data="fundsInfo"
        style="width: 100%"
        row-key="id"
        border
        lazy
        :load="fetchSysUser"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="fundsname" label="理财名称" />
        <el-table-column prop="remark" label="图标" #default="scope">
          <img :src="scope.row.remark" width="50" />
        </el-table-column>
        <el-table-column prop="rate" label="利率" />
        <el-table-column prop="term" label="期限(年)" />
        <!--        <el-table-column prop="tread" label="走势" />-->
        <el-table-column prop="rules" label="规则" />
        <el-table-column prop="share" label="库存" />
        <el-table-column label="操作" #default="scope">
          <el-button
            type="primary"
            v-show="scope.row.share == 0"
            @click="getBuyFunds(scope.row)"
          >
            购买
          </el-button>
        </el-table-column>
      </el-table>
    </el-dialog>
    <!--分页条-->
    <el-pagination
      v-model:current-page="pageParamsList.page"
      v-model:page-size="pageParamsList.limit"
      :page-sizes="[2, 20, 50, 100]"
      @size-change="fetchSysUser"
      @current-change="fetchSysUser"
      layout="total, sizes, prev, pager, next"
      :total="total"
    />
    <el-dialog
      v-model="dialogFundUserVisible"
      title="我购买的理财产品"
      width="70%"
    >
      <span>
        <el-table
          :data="findByUsers"
          style="width: 100%"
          row-key="id"
          border
          lazy
          :load="fetchSysUser"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <el-table-column prop="fundsname" label="理财名称" />
          <el-table-column prop="remark" label="图标" #default="scope">
            <img :src="scope.row.remark" width="50" />
          </el-table-column>
          <el-table-column prop="rate" label="利率" />
          <el-table-column prop="amount" label="买入金额" />
          <el-table-column prop="createTime" label="买入时间" />
          <el-table-column prop="updateTime" label="卖出时间" />
          <el-table-column prop="redundancy1" label="理财收益" />
          <el-table-column prop="state" label="理财状态" #default="scope">
            <el-tag v-if="scope.row.state == 1" type="danger">
              已抛出
            </el-tag>
            <el-tag v-if="scope.row.state == 0" type="success">
              正在理财
            </el-tag>
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot="scope">
              <el-button
                v-show="scope.row.state === 0"
                type="primary"
                @click="getUpdateFunds(scope.row)"
              >
                赎回
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFundUserVisible = false">取消</el-button>
          <el-button type="primary" @click="dialogFundUserVisible = false">
            确认
          </el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog v-model="dialogBuyFundsVisible" title="购买理财产品" width="30%">
      <span>
        <el-form :model="buyUser">
          <el-form-item v-show="false">
            <el-input v-model="buyUser.id" />
            <!--                  <el-input v-model="buyUser.id"/>-->
          </el-form-item>
          <el-form-item label="理财名称">
            <span>{{ buyUser.fundsname }}</span>
          </el-form-item>
          <el-form-item label="理财图标">
            <img :src="buyUser.remark" width="50" />
          </el-form-item>
          <el-form-item label="理财利率">
            <span>{{ buyUser.rate }}</span>
          </el-form-item>
          <el-form-item label="买入金额">
            <el-input v-model="buyUser.amount" @blur="BuyUserAmount" />
          </el-form-item>
        </el-form>
      </span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogBuyFundsVisible = false">取消</el-button>
          <el-button
            type="primary"
            @click="addFundsUser"
            :disabled="flag == true"
          >
            购买
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from 'vue'

import {
  findByUser,
  getAddFunds,
  getFunds,
  getFundsInfo,
  updateFunds,
} from '@/api/funds'
import { ElMessage, ElMessageBox } from 'element-plus'

const dialogVisible = ref(false)
const dialogFundUserVisible = ref(false)
const dialogBuyFundsVisible = ref(false)
//详情方法
const findFundsInfo = async row => {
  console.log(row)
  const { data } = await getFundsInfo(row.fid)
  fundsInfo.value = data
  dialogVisible.value = true
}

const buyUserFunds = {
  fundsname: '',
  remark: '',
  rate: '',
  amount: '',
  id: '',
  uid: '',
  cid: '',
}
const addUserFunds = reactive({
  fid: '',
  uid: '',
  amount: '',
})

const buyUser = ref(buyUserFunds)

const flag = ref(false)
const BuyUserAmount = () => {
  if (buyUser.value.amount <= 0) {
    ElMessage.error('金额不能小于0')
    flag.value = true
  }
  if (buyUser.value.amount > 0) {
    flag.value = false
  }
}

//购买
const getBuyFunds = async row => {
  buyUser.value = row
  buyUser.value.uid = localStorage.getItem('id')
  // alert(buyUser.value.fundsname)
  dialogBuyFundsVisible.value = true
}

const addFundsUser = async () => {
  addUserFunds.fid = buyUser.value.id
  addUserFunds.uid = buyUser.value.uid
  addUserFunds.amount = buyUser.value.amount
  // alert(addUserFunds.fid)

  const { code, message } = await getAddFunds(addUserFunds)
  if (code == 200) {
    ElMessage.success(message)
    dialogBuyFundsVisible.value = false
    dialogVisible.value = false
    fetchSysUser()
  } else {
    ElMessage.error(message)
  }
}

//赎回 需要用户id,产品id
const updateFundsUser = reactive({
  id: '',
  uid: '',
  fid: '',
})
//
const getUpdateFunds = row => {
  ElMessageBox.confirm('此操作将抛出购买的理财, 是否继续?', 'Warning', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    updateFundsUser.id = row.id
    updateFundsUser.uid = localStorage.getItem('id')
    updateFundsUser.fid = row.fid
    const { code, message } = await updateFunds(updateFundsUser)
    dialogFundUserVisible.value = false
  })
}
//查询理财产品
const myFunds = async () => {
  let item = localStorage.getItem('id')
  const { data } = await findByUser(item)
  findByUsers.value = data
  dialogFundUserVisible.value = true
}
const findByUsers = ref([])

const fundsInfo = ref([])

//搜索方法
const searchSysUser = () => {
  fetchSysUser(
    pageParamsUsers.value.page,
    pageParamsUsers.value.limit,
    form.value
  )
}

const resetData = () => {
  form.value.fname = ''
  fetchSysUser(
    pageParamsUsers.value.page,
    pageParamsUsers.value.limit,
    form.value
  )
}

const queryDto = {
  fname: '',
  risk: '1',
  yearrate: '2',
  mouthrate: '3',
  se: '',
}
const form = ref(queryDto)

const list = ref([])
const fetchSysUser = async () => {
  const { data } = await getFunds(
    pageParamsUsers.value.page,
    pageParamsUsers.value.limit,
    form.value
  )
  list.value = data.records
  total.value = data.total
}
onMounted(() => {
  fetchSysUser()
})
//分页数据
const pageParamsList = {
  page: 1,
  limit: 10,
}
const total = ref(0)
const pageParamsUsers = ref(pageParamsList)
</script>

<style scoped>
.demo-fit {
  display: flex;
  text-align: center;
  justify-content: space-between;
}
.demo-fit .block {
  flex: 1;
  display: flex;
  flex-direction: column;
  flex-grow: 0;
}

.demo-fit .title {
  margin-bottom: 10px;
  font-size: 14px;
  color: var(--el-text-color-secondary);
}
</style>
